package com.ruoyi.business.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.bizsys.service.IRepaymentOrderService;
import com.ruoyi.business.service.ICallbackService;
import com.ruoyi.business.service.MonnifyCallBackService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author cyh
 * @date 2022/6/11 11:10 上午
 */
@Slf4j
@RestController
@RequestMapping("/webhook/")
public class WebhookController {

    @Autowired
    IRepaymentOrderService iRepaymentOrderService;

    @Autowired
    ICallbackService callbackService;

    @Autowired
    MonnifyCallBackService monnifyCallBackService;


    @PostMapping("/monnify")
    @Log(title = "Monnify Webook 回调", businessType = BusinessType.CALLBACK)
    public AjaxResult monnifyWebhook(@RequestBody String res) {

        JSONObject callJson = JSON.parseObject(res);
        String eventType = callJson.getString("eventType");
        //收到转账    SUCCESSFUL_TRANSACTION
        //卡交易完成   SUCCESSFUL_TRANSACTION
        //线下支付    SUCCESSFUL_TRANSACTION
        if (eventType.equals("SUCCESSFUL_TRANSACTION")){
            monnifyCallBackService.webHookMonnifyTransaction(callJson);
        }
        //成功支付    SUCCESSFUL_DISBURSEMENT
        //支付失败 FAILED_DISBURSEMENT
        //成功退款 SUCCESSFUL_REFUND
        //退款失败 FAILED_REFUND
        //降沉 SETTLEMENT
        return AjaxResult.success();
    }

}
